package org.automated.realestatemanager.db.services;

import org.apache.log4j.Logger;
import org.automated.realestatemanager.db.entity.Publisher;

public class PublisherService extends CommonService<Publisher> {

	private final Logger log = Logger.getLogger(PublisherService.class);

	public PublisherService() {
		super(Publisher.class);
	}

	public Publisher mergePuplishers(Publisher pub1, Publisher pub2) {

		log.debug("/n****************************************");
		log.debug("/n****************************************");
		log.debug("/nMerging 2 publishers :" + pub1 + " \n and \n" + pub2);

		/*
		 * final String request1 =
		 * "UPDATE publ_phones SET publishers_publisher_id=" + pub2.getId() +
		 * " WHERE publishers_publisher_id=" + pub1.getId(); final String
		 * request2 = "UPDATE publ_adv SET publishers_publisher_id=" +
		 * pub2.getId() + " WHERE publishers_publisher_id=" + pub1.getId();
		 * final String request3 = "UPDATE advertisements SET publisher_id=" +
		 * pub2.getId() + " WHERE publisher_id=" + pub1.getId(); final String
		 * request4 = "DELETE FROM publishers WHERE publisher_id=" +
		 * pub1.getId(); // HibernateUtility.getSession().beginTransaction();
		 * 
		 * HibernateUtility.getSession().createSQLQuery(request1).executeUpdate()
		 * ;
		 * HibernateUtility.getSession().createSQLQuery(request2).executeUpdate
		 * ();
		 * HibernateUtility.getSession().createSQLQuery(request3).executeUpdate
		 * ();
		 * HibernateUtility.getSession().createSQLQuery(request4).executeUpdate
		 * ();
		 */

		pub1.getPhones().addAll(pub2.getPhones());
		pub1.getAdvertisements().addAll(pub2.getAdvertisements());
		pub2.getPhones().clear();
		pub2.getPhones().clear();

		merge(pub1);
		merge(pub2);
		// delete(get(pub2.getId()).getId());
		pub1 = get(pub1.getId());
		log.debug("Updated publisher " + pub1);
		return pub1;
	}

}
